package com.egao.common.module.company.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author warmwind
 * @createTime 2024-05-08 上午11:18
 */
public class JobUtil {



    public static List<Job> getJobList() {
        String jobJson = readJsonFromResource("job.json");
        List<Job> jobs = JSONObject.parseArray(jobJson, Job.class);
        List<Job> resultJobs = new ArrayList<Job>();
        for (Job job: jobs) {
            if (!Objects.isNull(job.getItems())) {
                resultJobs.addAll(job.getItems());
            }
        }
        return resultJobs;
    }

    public static String readJsonFromResource(String fileName) {
        InputStream is = Job.class.getClassLoader().getResourceAsStream(fileName);
        if (is == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}
